import pymysql

kcMesDb = pymysql.connect(host='localhost', port=23306, user='root', password='Pr0dkcd_', db="kcdoc")
kcMesjdbc = kcMesDb.cursor()

appDb = pymysql.connect(host='10.2.5.100', port=13306, user='root', password='kc@#2023++', db="kcdoc")
cursor = appDb.cursor()

def kcdoc_List(sql):
    returnData = []
    kcMesjdbc.execute(sql)
    fetchall = kcMesjdbc.fetchall()
    for e in fetchall:
        returnData.append(e)
    return returnData


def kcdoc_add_data(sql, data=[]):
    try:
        kcMesjdbc.execute(sql, data)
        kcMesjdbc.commit()
    except Exception as e:
        kcMesjdbc.rollback()
        print("Error", e.args[0])

def app_add_data(sql, data=[]):
    try:
        # 如果 data 是一个列表的列表/元组，则表示批量插入
        if isinstance(data[0], (list, tuple)):
            cursor.executemany(sql, data)
        else:
            cursor.execute(sql, data)

        # 提交事务
        appDb.commit()
    except Exception as e:
        # 出现异常时回滚事务
        cursor.rollback()
        print("Error:", e.args[0])

def kcdoc_search_one(sql):
    try:
        kcMesjdbc.execute(sql)
        res = kcMesjdbc.fetchone()
    except Exception as e:
        return "Error " + e.args[0]
    return res
